<template>
  <div class="chat-view-list-box">
    <div class="message-box" v-for="(item,index) in messageList" :key="item.id" :class="{rightmassage: item.userId!==user.id,animationClass} ">
      <div class="message-item">
        <div class="user-box">
          <div class="nickname">
            <span>{{item.nickname}}</span>
            <span>{{ item.creationTime}}</span>
          </div>
          <div class="img-box">
            <img :src="item.imgUrl">
          </div>
        </div>
        <div class="content">
          <div class="content-box">
            <span v-if="item.userId!==user.id&&index+1===messageList.length" id="contentspan">
            </span>
            <span v-else>
             {{  item.conversation}}
            </span>
          </div>

        </div>
      </div>
    </div>

  </div>
</template>

<script>
import {
  contentTypeit
} from '@/views/BlogFront/utils/plug'
export default {
  name: "chat-view-list",
  data() {
    return {
      messageList :[

        // {
        //   userId: "-1",
        //   nickname: "Chat🤖AI",
        //   imgUrl:  require("../../assets/chat/ChatGPT.png"),
        //   conversation: "的例子\\n\\n快速排序的例子： \\n给定一个整数数组[3,2,1,5,6,4]，使用快速排序法进行排序：\\n\\n首先选择数组中的一个元素作为基准（通常是数组的第一个元素），这里选择第一个元素3。剩余的元素被分为两个子序列，其中一个子序列包含所有小于基准的元素，另一个子序列",
        //   creationTime: new Date().toLocaleString()
        // }
      ],
      animationId: "",
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")): {},
    }
  },
  //计算属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    sendMessage(item) {
      this.messageList.push(item);
      this.chatgptRes(item.conversation)
    },
    chatgptRes(conversation){
        this.request.get("/ChatAPI/chat",{
          params:{
            prompt: conversation
          }
        }).then(res=>{
          this.$emit('clearLoad');
          let  ChatGptObject={
            userId: "-1",
            nickname: "Chat🤖AI",
            imgUrl:  require("../../assets/chat/ChatGPT.png"),
            conversation: res.choices[0].text,
            creationTime: new Date().toLocaleString()
          }
          this.contentText(ChatGptObject);
          console.log("chatgpt返回的数据",ChatGptObject)
          this.messageList.push(ChatGptObject)
          this.$emit("scrollbarBT");
          //更新到vuex
          this.$store.state.ChatGPTMessageList= this.messageList;
        })
    },
    contentText(messageItem){
      var timer = setTimeout(()=> {
        contentTypeit("#contentspan",messageItem.conversation); //打字机效果
        clearTimeout(timer);
      }, 1500);
    }
  },
  //生命周期 一 创建完成(可以访问当前this实例)
  created() {
    //查看当前vuex中有没有历史数据
    this.messageList = this.$store.state.ChatGPTMessageList
  },
  //生命周期 一 挂载完成(可以访问DOM元素)
  mounted() {

  },
  //生命周期 - 创建之前
  beforeCreate() {
  },
  //生命周期 - 挂载之前
  beforeMount() {
  },
  //生命周期 - 更新之前
  beforeUpdate() {
  },
  //生命周期 - 更新之后
  updated() {
  },
  //生命周期 - 销毁之前
  beforeDestroy() {
  },
  //生命周期 - 销毁完成
  destroyed() {
  },
  //如果页面有keep-alive缓存功能，这个函数会触发
  activated() {
  },
}
</script>

<style lang="less" scoped>
.chat-view-list-box{
  width: 100%;
  height: 100%;
  .message-box{
    width: 100%;
    display: flex;
    justify-content: flex-end ;


    .message-item{
      width: auto;
      margin-bottom: 20px;
      display: flex;
      flex-direction: column;

      .user-box{
        display: flex;
        align-items: center;
        justify-content: flex-end;
        flex-grow:0;
        .nickname{
          font-size: 15px;
          color: white;
          span{
            margin-right: 10px;
          }
        }
        .img-box{
          width: 45px;
          height: 45px;
          img{
            width: 100%;
            height: 100%;
            border-radius: 50%;
          }
        }
      }
      .content{

        margin-top: 15px;
        max-width: 1000px;
        display: flex;
        justify-content: flex-end;
        .content-box{
          display: inline-block;
          max-width: 1000px;
          padding: 15px 15px;
          background: #409eff;
          font-size: 17px;
          color: white;
          font-weight: 500;
          border-radius: 15px 1px 15px 15px;
          span{
            line-height: 25px;
            letter-spacing: 1px;
            white-space:pre-line;
            word-wrap:break-word;
          }
        }


      }
    }


  }
  .rightmassage{
    justify-content: flex-start !important;

    .message-item{
      .user-box{
        justify-content: flex-start;
        .img-box{
          order:-1;
        }
      }
      .content{
        justify-content: flex-start !important;

        .content-box{
          border-radius: 1px 15px 15px 15px !important;
          background: #393c4b !important;
        }


      }

    }
  }


}



/*base code*/
.animated {
  -webkit-animation-duration: 1s;
  animation-duration: 1s;
  -webkit-animation-fill-mode: both;
  animation-fill-mode: both;
}
.animated.infinite {
  -webkit-animation-iteration-count: infinite;
  animation-iteration-count: infinite;
}
.animated.hinge {
  -webkit-animation-duration: 2s;
  animation-duration: 2s;
}
/*the animation definition*/
@-webkit-keyframes bounceInUp {
  0%, 100%, 60%, 75%, 90% {
    -webkit-transition-timing-function: cubic-bezier(0.215, .61, .355, 1);
    transition-timing-function: cubic-bezier(0.215, .61, .355, 1)
  }
  0% {
    opacity: 0;
    -webkit-transform: translate3d(0, 3000px, 0);
    transform: translate3d(0, 3000px, 0)
  }
  60% {
    opacity: 1;
    -webkit-transform: translate3d(0, -20px, 0);
    transform: translate3d(0, -20px, 0)
  }
  75% {
    -webkit-transform: translate3d(0, 10px, 0);
    transform: translate3d(0, 10px, 0)
  }
  90% {
    -webkit-transform: translate3d(0, -5px, 0);
    transform: translate3d(0, -5px, 0)
  }
  100% {
    -webkit-transform: translate3d(0, 0, 0);
    transform: translate3d(0, 0, 0)
  }
}
@keyframes bounceInUp {
  0%, 100%, 60%, 75%, 90% {
    -webkit-transition-timing-function: cubic-bezier(0.215, .61, .355, 1);
    transition-timing-function: cubic-bezier(0.215, .61, .355, 1)
  }
  0% {
    opacity: 0;
    -webkit-transform: translate3d(0, 3000px, 0);
    -ms-transform: translate3d(0, 3000px, 0);
    transform: translate3d(0, 3000px, 0)
  }
  60% {
    opacity: 1;
    -webkit-transform: translate3d(0, -20px, 0);
    -ms-transform: translate3d(0, -20px, 0);
    transform: translate3d(0, -20px, 0)
  }
  75% {
    -webkit-transform: translate3d(0, 10px, 0);
    -ms-transform: translate3d(0, 10px, 0);
    transform: translate3d(0, 10px, 0)
  }
  90% {
    -webkit-transform: translate3d(0, -5px, 0);
    -ms-transform: translate3d(0, -5px, 0);
    transform: translate3d(0, -5px, 0)
  }
  100% {
    -webkit-transform: translate3d(0, 0, 0);
    -ms-transform: translate3d(0, 0, 0);
    transform: translate3d(0, 0, 0)
  }
}
.bounceInUp {
  -webkit-animation-name: bounceInUp;
  animation-name: bounceInUp
}



/*base code*/
.animated {
  -webkit-animation-duration: 1s;
  animation-duration: 1s;
  -webkit-animation-fill-mode: both;
  animation-fill-mode: both;
}
.animated.infinite {
  -webkit-animation-iteration-count: infinite;
  animation-iteration-count: infinite;
}
.animated.hinge {
  -webkit-animation-duration: 2s;
  animation-duration: 2s;
}
/*the animation definition*/
@-webkit-keyframes fadeInUp {
  0% {
    opacity: 0;
    -webkit-transform: translate3d(0, 100%, 0);
    transform: translate3d(0, 100%, 0)
  }
  100% {
    opacity: 1;
    -webkit-transform: none;
    transform: none
  }
}
@keyframes fadeInUp {
  0% {
    opacity: 0;
    -webkit-transform: translate3d(0, 100%, 0);
    -ms-transform: translate3d(0, 100%, 0);
    transform: translate3d(0, 100%, 0)
  }
  100% {
    opacity: 1;
    -webkit-transform: none;
    -ms-transform: none;
    transform: none
  }
}
.fadeInUp {
  -webkit-animation-name: fadeInUp;
  animation-name: fadeInUp
}


</style>